package orderOpe;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import common.Cart;
import common.Customer;
import common.Item;
import common.Order;
import common.OrderItem;

public class JudgeServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		//orderCheck.jspから値を受け取る
		String all = req.getParameter("all");
		String feee = req.getParameter("deliver");
		String deliver = req.getParameter("hope");
		String costtotal = req.getParameter("cost_total");
		String pricetotal = req.getParameter("total");
		//String order_num =Number.getOrder();
		int judge =0;
		StringBuffer msg = new StringBuffer();
		
		//クラス情報取得					
		HttpSession session = req.getSession( );
		Customer cs = (Customer)session.getAttribute("customer");
		Cart cart = (Cart)session.getAttribute("opecart");
		
		//カートに情報を入れる
		int iDeliver = Integer.parseInt(deliver);
		Date date= new Date();
		long t = date.getTime();
		t = t + (iDeliver * 1000 * 60 * 60 * 24);
		date.setTime(t);
		cart.setDeli(date);
		int fee=Integer.parseInt(feee);
		cart.setFee(fee);
		int price_total=Integer.parseInt(pricetotal);
		cart.setPrice_total(price_total);
		int cost_total=Integer.parseInt(costtotal);
		cart.setCost_total(cost_total);
		cart.setJudge(judge);
		//cart.setOrder_num(order_num);
		cart.setCs(cs);
		
		//Customerから値を受け取る	
		String card = cs.getCard();
		
		//④カード会社に合計請求金額と名前、カード番号を送信													
			//カードの与信();			
			//エラーメッセージをセットする
		
			if(card == null){
				msg.append("カード番号が登録されてません。\n");
			}
			
		String path = "/operator/judge_ng.jsp";
		judge=0;
		//oｋなら出荷管理に登録	
		if ( msg.length() == 0 ) {
			
			final String URL_INQUIRY = "http://itensv:8080/Payment/Inquiry"; 
			//問い合わせ先の URL 
			URL url = new URL(URL_INQUIRY); 
			//決済代行業者のシステムに接続する 
			HttpURLConnection connection = null; 
			connection = (HttpURLConnection) url.openConnection(); 
			//POST メソッドで送信する 
			connection.setRequestMethod("POST"); 
			connection.setDoOutput(true); 
			//パラメータを設定 
			// number : クレジットカード番号 
			// amount : 金額 
			// account : アカウント 
			// password : パスワード 
			StringBuffer param = new StringBuffer(); 
			param.append("number=" + card); 
			param.append("&amount=" + all); 
			param.append("&account=" + "iten7"); 
			param.append("&password=" + "hZ5A82iN"); 
			//パラメータを送信する 
			PrintWriter printWriter = new PrintWriter(connection.getOutputStream()); 
			printWriter.print(param.toString()); 
			printWriter.close(); 
			//結果を取得する 

			switch (connection.getResponseCode()) { 
			case HttpURLConnection.HTTP_OK: 
			 //与信 OK 
				path = "/operator/judge_ok.jsp";
				cart.setJudge(1);
			 break; 
			case HttpURLConnection.HTTP_ACCEPTED: 
			 //与信 NG 
			 break; 
			case HttpURLConnection.HTTP_BAD_REQUEST: 
			 //パラメータが間違っている 
			 break; 
			}
		}
		
		else {
			req.setAttribute("msg", msg.toString());
		}
		
		//DBに各情報を登録
		Order.add(cart);
		
		if(judge == 0){
			List<OrderItem> list = cart.getItems();
			for(int i = 0; i < list.size(); i++){
				OrderItem oi = list.get(i);
				Item item = Item.find_by_code(oi.getItem_code());
				//int orders=cart.getOrders();
				item.setStock(oi.getOrders());	
			}
		}
		//遷移
		RequestDispatcher dispatcher = 
				req.getRequestDispatcher(path);
		dispatcher.forward(req, resp);
	}
}
